﻿///////////////////////////////////////////////////////
//  UpdatePanelExtender 1.0
///////////////////////////////////////////////////////
Type.registerNamespace('AfterEdge.Web.UI');

AfterEdge.Web.UI.UpdatePanelExtender = function(element)
{
    AfterEdge.Web.UI.UpdatePanelExtender.initializeBase(this, [element]);
    
    this._validationGroup = null;
    this._causesValidation = null;
    this._commandArgument = null;
    this._pageRequestManager = null;
    
    // These match the events exposed from teh PageReqeustManager
    this._initializeRequestHandler = null;
    this._beginRequestHandler = null;
    this._pageLoadingHandler = null;
    this._pageLoadedHandler = null;
    this._endRequestHandler = null;
}

AfterEdge.Web.UI.UpdatePanelExtender.prototype = 
{
    initialize: function()
    {
        window[this.get_id()] = this;
        
        // Register some event handlers
        this._initializeRequestHandler = Function.createDelegate(this, this._onInitializeRequest);;
        this._beginRequestHandler = Function.createDelegate(this, this._onBeginReqeust);
        this._pageLoadingHandler = Function.createDelegate(this, this._onPageLoading);
        this._pageLoadedHandler = Function.createDelegate(this, this._onPageLoaded);
        this._endRequestHandler = Function.createDelegate(this, this._onEndRequest);       
        
        // Get the current PageRequestManager
        this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
        // Hook up the events to the extender
        this._pageRequestManager.add_initializeRequest(this._initializeRequestHandler);
        this._pageRequestManager.add_beginRequest(this._beginRequestHandler);
        this._pageRequestManager.add_pageLoading(this._pageLoadingHandler);
        this._pageRequestManager.add_pageLoaded(this._pageLoadedHandler);
        this._pageRequestManager.add_endRequest(this._endRequestHandler);
        
        AfterEdge.Web.UI.UpdatePanelExtender.callBaseMethod(this, 'initialize');
    },
    
    dispose: function()
    {
        // Dispose the hevent handlers
        this._pageRequestManager.remove_initializeRequest(this._initializeRequestHandler);
        this._pageRequestManager.remove_beginRequest(this._beginRequestHandler);
        this._pageRequestManager.remove_pageLoading(this._pageLoadingHandler);
        this._pageRequestManager.remove_pageLoaded(this._pageLoadedHandler);
        this._pageRequestManager.remove_endRequest(this._endRequestHandler);

        this._initializeRequestHandler = null;
        this._beginRequestHandler = null;
        this._pageLoadingHandler = null;
        this._pageLoadedHandler = null;
        this._endRequestHandler = null;
        
        AfterEdge.Web.UI.UpdatePanelExtender.callBaseMethod(this, 'dispose');
    },
    
    // Events
    
    add_initializeRequest: function(handler)
    {
        this.get_events().addHandler('initializeRequest', handler);
    },
    
    remove_initializeRequest: function(handler)
    {
        this.get_events().removeHandler('initializeRequest', handler);
    },
    
    add_beginRequest: function(handler)
    {
        this.get_events().addHandler('beginReqeust', handler);
    },
    
    remove_beginRequest: function(handler)
    {
        this.get_events().removeHandler('beginReqeust', handler);
    },
    
    add_pageLoading: function(handler)
    {
        this.get_events().addHandler('pageLoading', handler);
    },
    
    remove_pageLoading: function(handler)
    {
        this.get_events().removeHandler('pageLoading', handler);
    },
    
    add_pageLoaded: function(handler)
    {
        this.get_events().addHandler('pageLoaded', handler);
    },
    
    remove_pageLoaded: function(handler)
    {
        this.get_events().removeHandler('pageLoaded', handler);
    },
    
    add_endRequest: function(handler)
    {
        this.get_events().addHandler('endRequest', handler);
    },
    
    remove_endRequest: function(handler)
    {
        this.get_events().removeHandler('endRequest', handler);
    },
    
    // Event Handler
    _onInitializeRequest: function(sender, args)
    {
        // Only fire the event if the postBackElement matches this extender's element
        if(args.get_postBackElement != null && args.get_postBackElement().id != this.get_id())
            return;
            
        var handler = this.get_events().getHandler('initializeRequest');

        if(handler)
            handler(this, args);
    },
    
    _onBeginReqeust: function(sender, args)
    {
        // Only fire the event if the postBackElement matches this extender's element
        if(args.get_postBackElement != null && args.get_postBackElement().id != this.get_id())
            return;
            
        var handler = this.get_events().getHandler('beginReqeust');

        if(handler)
            handler(this, args);
    },
    
    _onPageLoading: function(sender, args)
    {
        // Only fire the event if this extenders panel is included in the updating panels
        if(!Array.contains(args.get_panelsUpdating(), this.get_element()))
            return;
        
        var handler = this.get_events().getHandler('pageLoading');

        if(handler)
            handler(this, args);
    },
    
    _onPageLoaded: function(sender, args)
    {
        // Only fire the event if this extenders panel is included in the updated panels
        if(!Array.contains(args.get_panelsUpdated(), this.get_element()))
            return;
        
        var handler = this.get_events().getHandler('pageLoaded');

        if(handler)
            handler(this, args);
    },
    
    _onEndRequest: function(sender, args)
    {
        var handler = this.get_events().getHandler('endRequest');

        if(handler)
            handler(this, args);
    },
        
    // Properties
    get_validationGroup: function()
    {
        return this._validationGroup;
    },
    
    set_validationGroup: function(value)
    {
        this._validationGroup = value;
    },
    
    get_causesValidation: function()
    {
        return this._causesValidation;
    },
    
    set_causesValidation: function(value)
    {
        this._causesValidation = value;
    },
    
    get_isInAsyncPostBack: function()
    {
        this._pageRequestManager.get_isInAsyncPostBack();
    },
    
    get_commandArgument: function()
    {
        return this._commandArgument;
    },
    
    set_commandArgument: function(value)
    {
        this._commandArgument = value;
    },
    
    // Methods
    
    postback: function(arg)
    {       
        var eventTarget = this.get_id().replace(/_/g, '$');
        var commandArgument = typeof(arg) == 'undefined' ? this.get_commandArgument() : arg;
        
        if(this.get_causesValidation() === true)
        {
            var options = new WebForm_PostBackOptions(eventTarget, commandArgument, this.get_causesValidation(), this.get_validationGroup(), '', false, true);
            WebForm_DoPostBackWithOptions(options);
        }
        else
        {
            __doPostBack(eventTarget, commandArgument);
        }
    },
    
    abortPostback: function()
    {
        this._pageRequestManager.abortPostBack();
    }    
}

AfterEdge.Web.UI.UpdatePanelExtender.registerClass('AfterEdge.Web.UI.UpdatePanelExtender', Sys.UI.Behavior, Sys.IDisposable);

